<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------
/**
 * Created by PhpStorm.
 * Script Name: Index.php
 * Create: 2020/7/22 18:02
 * Description: 小程序数据分析与统计
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\mini\controller;

use ky\MiniPlatform\Request\DataCubeGetWeAnalysisAppidDailyVisitTrend;
use ky\MiniPlatform\RequestClient;

class Index extends Base
{
    protected $needAppAuth = true;
    /**
     * @var RequestClient
     */
    private $client;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->client = new RequestClient();
    }

    /**
     * 访问统计
     * @return mixed
     * Author: fudaoji<fdj@kuryun.cn>
     * @throws \Exception
     */
    public function index(){
        $yesterday_date = date('Ymd', strtotime('-1 days', time()));
        $yesterday = [
            'session_cnt' => 0, //打开次数
            'visit_pv' => 0,  //访问次数
            'visit_uv' => 0, //访问人数
            'visit_uv_new' => 0, //新用户数
            'stay_time_uv' => 0,  //人均停留时长(秒)
            'stay_time_session' => 0 //次均停留时长(秒)
        ];

        if($this->miniInfo['is_auth']){
            $request = new DataCubeGetWeAnalysisAppidDailyVisitTrend();
            $request->setBeginDate($yesterday_date);
            $request->setEndDate($yesterday_date);
            $response = $this->client->execute($request, $this->getAccessToken());


            if(isset($response['list'])){
                if(count($response['list'])){
                    $yesterday = $response['list'][0];
                }
            }
        }

        $this->assign['yesterday'] = $yesterday;
        return $this->show();
    }
}